package com.travel.modules.review.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.travel.commons.shiro.ShiroUserGetter;
import com.travel.commons.utils.R;
import com.travel.commons.validator.ValidatorUtils;
import com.travel.commons.validator.groups.AddGroup;
import com.travel.modules.alipay.entity.OrderForm;
import com.travel.modules.alipay.service.OrderFormService;
import com.travel.modules.asset.service.GoodsService;
import com.travel.modules.review.entity.Review;
import com.travel.modules.review.service.ReviewService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 评论功能
 */
@RestController
@RequestMapping("/review")
public class ReviewController {
    @Resource
    private GoodsService goodsService;

    @Resource
    private ReviewService reviewService;

    @Resource
    private OrderFormService orderFormService;

    /**
     * 获取评论
     * @param param goodsId - 商品id
     * @return 该商品下的评论
     */
    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> param) {
        String goodsId = (String) param.get("goodsId");
        if (StringUtils.isBlank(goodsId)) {
            return R.error("缺少商品编号");
        }
        List<Review> list = reviewService.getReviewList(goodsId);
        return R.ok().put("page", list);
    }

    @RequestMapping("/list/current")
    public R list2Current() {
        List<Review> list = reviewService.list();
        return R.ok().put("page", list);
    }

    /**
     * 添加评论
     * @param form {@link com.travel.modules.review.entity.Review}
     */
    @RequestMapping("/add")
    public R add(@RequestBody Review form) {
        ValidatorUtils.validate(form, AddGroup.class);
        String orderId = form.getOrderId();
        if (StringUtils.isBlank(orderId)) {
            return R.error("缺少订单号");
        }
        OrderForm orderForm = orderFormService.getOne(new QueryWrapper<OrderForm>().eq("order_id", orderId));
        if (orderForm == null) {
            return R.error("订单不存在");
        }
        orderForm.setStatus(4);
        Integer satisfaction = form.getSatisfaction();
        if (satisfaction >= 2) {
            String goodsId = form.getGoodsId();
            Object goodsObj = goodsService.getGoodsObject(goodsId);
            if (goodsObj == null) {
                return R.error("商品不存在");
            }
            goodsService.addReputation(goodsObj, form.getGoodsId());
        }
        form.setUserId(ShiroUserGetter.getUserId());
        orderFormService.updateById(orderForm);
        reviewService.save(form);
        return R.ok();
    }

    /**
     * 删除评论
     * @param form goodsId - 商品id
     *              id - 评论id
     */
    @RequestMapping("/del")
    public R del(@RequestBody Review form) {
        Integer id = form.getId();
        if (id == null) {
            return R.error("缺少评论id");
        }
        Review review = reviewService.getById(id);
        if (ShiroUserGetter.getUserId().compareTo(review.getUserId()) != 0) {
            return R.error("不能删除他人评论");
        }
        reviewService.removeById(id);
        return R.ok();
    }

    /**
     * 清理商品下的所有评论
     * @param param goodsId - 商品id
     */
    @RequestMapping("/clean")
    public R clean(@RequestBody Map<String, Object> param) {
        if (ShiroUserGetter.getUser().getRoleId() != 1) {
            return R.error("没有权限");
        }
        String goodsId = (String) param.get("goodsId");
        if (StringUtils.isBlank(goodsId)) {
            return R.error("商品id不能为空");
        }
        reviewService.remove(new QueryWrapper<Review>().eq("goods_id", goodsId));
        return R.ok();
    }
}
